<?php

/**
 * sfAudit filter form.
 *
 * @package    issexpress.plugins
 * @subpackage sfPropelAuditPlugin.lib.filter
 * @author     Alan Pieckenstainer
 */
class sfAuditFormFilter extends BasesfAuditFormFilter {

    /**
     * Define configure function
     * @package    issexpress.plugins
     * @subpackage sfPropelAuditPlugin.lib.filter
     */
    public function configure() {
        $this->widgetSchema['remote_ip_address']->setOption('with_empty', false);
        $this->widgetSchema['object']->setOption('with_empty', false);
        $this->widgetSchema['object_key']->setOption('with_empty', false);
        $this->widgetSchema['object_changes']->setOption('with_empty', false);
        $this->widgetSchema['query']->setOption('with_empty', false);
        $this->widgetSchema['user']->setOption('with_empty', false);
        $this->widgetSchema['type']->setOption('with_empty', false);

        $defaults = $this->getDefaults();

        $name='created_at';
        $this->widgetSchema[$name]->getOption('from_date')->setAttribute('class',"date_align");
        $this->widgetSchema[$name]->getOption('to_date')->setAttribute('class',"date_align");
        $this->widgetSchema[$name]->setOption('with_empty', false);
        $this->setDefault($name, array('from'=>array('text'=>(isset($defaults[$name]['from']) ? $defaults[$name]['from'] : '')),'to'=>array('text'=> (isset($defaults[$name]['to']) ? $defaults[$name]['to'] : ''))));
    }

    /**
     * Redefine setup function
     * @package    issexpress.plugins
     * @subpackage sfPropelAuditPlugin.lib.filter
     */
    public function setup()
  {
        parent::setup();

     
    $this->setWidgets(array(
      'remote_ip_address' => new sfWidgetFormFilterInput(),
      'object'            => new sfWidgetFormFilterInput(),
      'object_key'        => new sfWidgetFormFilterInput(),
      'object_changes'    => new sfWidgetFormFilterInput(),
      'query'             => new sfWidgetFormFilterInput(),
      'user'              => new sfWidgetFormFilterInput(),
      'type'              => new sfWidgetFormFilterInput(),
      'created_at'        => new sfWidgetFormFilterDate(array('from_date' => sfWidgetFormDateJQueryUI(), 'to_date' => sfWidgetFormDateJQueryUI(), 'with_empty' => false, 'template' => $i18n_context->__('Desde %from_date% Hasta %to_date%'))),
      'audit_day'         => new sfWidgetFormInputCheckbox(array('label'=>sfContext::getInstance()->getI18N()->__('Only today registries',array(),'messages'))),
        ));

    $this->setValidators(array(
      'remote_ip_address' => new sfValidatorSchemaFilter('text', new sfValidatorString(array('required' => false))),
      'object'            => new sfValidatorSchemaFilter('text', new sfValidatorString(array('required' => false))),
      'object_key'        => new sfValidatorSchemaFilter('text', new sfValidatorString(array('required' => false))),
      'object_changes'    => new sfValidatorSchemaFilter('text', new sfValidatorString(array('required' => false))),
      'query'             => new sfValidatorSchemaFilter('text', new sfValidatorString(array('required' => false))),
      'user'              => new sfValidatorSchemaFilter('text', new sfValidatorString(array('required' => false))),
      'type'              => new sfValidatorSchemaFilter('text', new sfValidatorString(array('required' => false))),
'created_at'        => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDate(array('required' => false)))),
      'audit_day'         => new sfValidatorBoolean(),
        ));

    $this->widgetSchema->setNameFormat('sf_audit_filters[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    
  }
    


      public function getFields()
  {
    return array(
      'id'                => 'Number',
      'remote_ip_address' => 'Text',
      'object'            => 'Text',
      'object_key'        => 'Text',
      'object_changes'    => 'Text',
      'query'             => 'Text',
      'user'              => 'Text',
      'type'              => 'Text',
      'created_at'        => 'Date',
      'audit_day'        => 'Text',
    );
  }


    /**
     * addCreatedAtColumnCriteria function
     * Specifies the criteria that applies for the field CREATED_AT
     * @package    issexpress.plugins
     * @subpackage sfPropelAuditPlugin.lib.filter
     * @param Criteria $criteria
     * @param string  $field
     * @param array  $values
     */
    protected function addCreatedAtColumnCriteria(Criteria $criteria, $field, $values) {

        $name=sfAuditPeer::CREATED_AT;
var_dump($values);exit();
        //Fix Day hours
        $beginDayTime = "00:00:00";
        $endDayTime = "23:59:59";
        //Fix de Culture
        $culture = sfContext::getInstance()->getUser()->getCulture();
        if ($culture == 'en') {
            $culture = 'en_US';
        }else if ($culture =='es') {
            $culture = 'es_AR';
        }
        //Fix ANSI
        if (!empty($values['from'])) {
            list($day, $month, $year) = sfContext::getInstance()->getI18N()->getDateForCulture($values['from'], $culture);
            $fdate = $year.'-'.$month.'-'.$day." ".$beginDayTime;
            $criteria->addAnd($name, $fdate, Criteria::GREATER_EQUAL);
        }
        //Fix ANSI
        if (!empty($values['to'])) {
            list($day, $month, $year) = sfContext::getInstance()->getI18N()->getDateForCulture($values['to'], $culture);
            $todate = $year.'-'.$month.'-'.$day." ".$endDayTime;
            $criteria->addAnd($name, $todate, Criteria::LESS_EQUAL);
        }
    }


       /**
     * addTicketsDayColumnCriteria function
     * Specifies the criteria that applies for day's tickets created
     * @package    issserver.lib
     * @subpackage filter
     * @param Criteria $criteria
     * @param string  $field
     * @param array  $values
     */
    protected function addAuditDayColumnCriteria(Criteria $criteria, $field, $values) {

        if ($values!=false) {

            $today=getdate();

            $year=$today['year'];
            $month=$today['mon'];
            $day=$today['mday'];

            if($month<10) {
                $month="0".$month;
            }
            if($day<10) {
                $day="0".$day;
            }
            $date=$year."-".$month."-".$day;

            $criteria->add(sfAuditPeer::CREATED_AT, $date."%",Criteria::LIKE);
        }
    }

    /**
     * addRemoteIpAddressColumnCriteria function
     * Specifies the criteria that applies for the field REMOTE_IP_ADDRESS
     * @package    issexpress.plugins
     * @subpackage sfPropelAuditPlugin.lib.filter
     * @param Criteria $criteria
     * @param string  $field
     * @param array  $values
     */
    protected function addRemoteIpAddressColumnCriteria(Criteria $criteria, $field, $values) {
        if (!empty($values['text'])) {
            $criteria->add(sfAuditPeer::REMOTE_IP_ADDRESS, $values['text']."%", CRITERIA::LIKE);
        }
    }

    /**
     * addObjectColumnCriteria function
     * Specifies the criteria that applies for the field OBJECT
     * @package    issexpress.plugins
     * @subpackage sfPropelAuditPlugin.lib.filter
     * @param Criteria $criteria
     * @param string  $field
     * @param array  $values
     */
    protected function addObjectColumnCriteria(Criteria $criteria, $field, $values) {
        if (!empty($values['text'])) {
            $criteria->add(sfAuditPeer::OBJECT, $values['text'].'%',Criteria::LIKE);
        }
    }

    /**
     * addObjectKeyColumnCriteria function
     * Specifies the criteria that applies for the field OBJECT_KEY
     * @package    issexpress.plugins
     * @subpackage sfPropelAuditPlugin.lib.filter
     * @param Criteria $criteria
     * @param string  $field
     * @param array  $values
     */
    protected function addObjectKeyColumnCriteria(Criteria $criteria, $field, $values) {
        if (!empty($values['text'])) {
            $criteria->add(sfAuditPeer::OBJECT_KEY, $values['text']);
        }
    }

    /**
     * addObjectChangesColumnCriteria function
     * Specifies the criteria that applies for the field OBJECT_CHANGES
     * @package    issexpress.plugins
     * @subpackage sfPropelAuditPlugin.lib.filter
     * @param Criteria $criteria
     * @param string  $field
     * @param array  $values
     */
    protected function addObjectChangesColumnCriteria(Criteria $criteria, $field, $values) {
        if (!empty($values['text'])) {
            $criteria->add(sfAuditPeer::OBJECT_CHANGES, '%'.$values['text'].'%',Criteria::LIKE);
        }
    }

    /**
     * addQueryColumnCriteria function
     * Specifies the criteria that applies for the field QUERY
     * @package    issexpress.plugins
     * @subpackage sfPropelAuditPlugin.lib.filter
     * @param Criteria $criteria
     * @param string  $field
     * @param array  $values
     */
    protected function addQueryColumnCriteria(Criteria $criteria, $field, $values) {
        if (!empty($values['text'])) {
            $criteria->add(sfAuditPeer::QUERY, $values['text'].'%',Criteria::LIKE);
        }
    }

    /**
     * addUserColumnCriteria function
     * Specifies the criteria that applies for the field USER
     * @package    issexpress.plugins
     * @subpackage sfPropelAuditPlugin.lib.filter
     * @param Criteria $criteria
     * @param string  $field
     * @param array  $values
     */
    protected function addUserColumnCriteria(Criteria $criteria, $field, $values) {
        if (!empty($values['text'])) {
            $criteria->add(sfAuditPeer::USER, $values['text'].'%',Criteria::LIKE);
        }

    }

    /**
     * addTypeColumnCriteria function
     * Specifies the criteria that applies for the field TYPE
     * @package    issexpress.plugins
     * @subpackage sfPropelAuditPlugin.lib.filter
     * @param Criteria $criteria
     * @param string  $field
     * @param array  $values
     */
    protected function addTypeColumnCriteria(Criteria $criteria, $field, $values) {
        if (!empty($values['text'])) {
            $criteria->add(sfAuditPeer::TYPE, $values['text'].'%',Criteria::LIKE);
        }
    }
}